package com.addict.applocker.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.addict.applocker.R;
import com.addict.applocker.manager.SettingManager;

public class SettingActivity extends AppCompatActivity{
    private Toolbar mcToolbar = null;
    private TextView mcSetTheme = null;
    private Switch mcSetOpenLocker = null;
    private Switch mcSetLockerSite = null;
    private NumberPicker mcSetLockerWidth = null;
    private Switch mcScreenLock = null;
    private Switch mcShutDown = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        mcToolbar = (Toolbar) findViewById(R.id.toolbar);
        mcSetTheme = (TextView) findViewById(R.id.ctl_set_theme);
        mcSetOpenLocker = (Switch) findViewById(R.id.ctl_open_locker);
        mcSetLockerSite = (Switch) findViewById(R.id.ctl_locker_site);
        mcSetLockerWidth = (NumberPicker) findViewById(R.id.ctl_locker_width);
        mcScreenLock = (Switch) findViewById(R.id.ctl_screenlock);
        mcShutDown = (Switch) findViewById(R.id.ctl_shutdown);

        // 设置一下NumberPicker的最大最小值
        mcSetLockerWidth.setMinValue(0);
        mcSetLockerWidth.setMaxValue(10);

        // 设置一下toolbar属性
        mcToolbar.inflateMenu(R.menu.menu_setting);
        mcToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mcToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId())
                {
                    case R.id.action_ok:
                        saveSetting();
                        Toast.makeText(getApplicationContext(), "设置已保存",
                                Toast.LENGTH_SHORT).show();
                        finish();
                        break;
                }
                return true;
            }
        });

        // 选择主题颜色需要跳转到其它页面
        mcSetTheme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SettingActivity.this, SelectThemeActivity.class));
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initSetting();
    }

    private void initSetting() {
        // 咳咳，纯洁点！不要乱想
        SettingManager sm = SettingManager.getInstance();
        mcToolbar.setBackgroundColor(sm.getThemeColor());
        mcSetOpenLocker.setChecked(sm.isOpenLocker());
        mcSetLockerSite.setChecked(sm.getLockerSite() == SettingManager.Locker_Site.Left);
        mcSetLockerWidth.setValue(sm.getLockerWidth());
        mcScreenLock.setChecked(sm.isScreenLockOff());
        mcShutDown.setChecked(sm.isShutDownOff());
    }

    private void saveSetting() {
        SettingManager sm = SettingManager.getInstance();
        sm.setOpenLocker(mcSetOpenLocker.isChecked());
        sm.setLockerSite(mcSetLockerSite.isChecked() ?
                            SettingManager.Locker_Site.Left :
                            SettingManager.Locker_Site.Right);
        sm.setLockerWidth(mcSetLockerWidth.getValue());
        sm.setScreenLockOff(mcScreenLock.isChecked());
        sm.setShutDownOff(mcShutDown.isChecked());
        sm.write();
    }
}
